// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Data;

namespace SnapObjects.Data
{
    internal class DataTableXmlImporter : IDataImporter
    {
        private readonly IDataTableImporter _importer;
        private readonly DataFormat _dataFormat = DataFormat.Xml;

        public DataTableXmlImporter(DataTable dataTable)
        {
            _importer = new DataTableImporter(dataTable);
        }
        public DataTableXmlImporter(IDataTableImporter importer)
        {
            _importer = importer;
        }

        public int Import(string text)
        {
            return this.Import(text, -1);
        }

        public int Import(string text, int startRow)
        {
            return this.Import(text, startRow, -1);
        }

        public int Import(string text, int startRow, int endRow)
        {
            return this.Import(text, startRow, endRow, -1);
        }

        public int Import(string text, int startRow, int endRow, int startProperty)
        {
            return this.Import(text, startRow, endRow, startProperty, -1);
        }

        public int Import(string text, int startRow, int endRow, int startProperty, int endProperty)
        {
            var setting = new DataFormatSetting()
            {
                StartRow = startRow,
                EndRow = endRow,
                StartProperty = startProperty,
                EndProperty = endProperty
            };

            return _importer.Import(text, _dataFormat, setting);
        }

        public int ImportByKey(string text)
        {
            return this.ImportByKey(text, -1);
        }

        public int ImportByKey(string text, int startRow)
        {
            return this.ImportByKey(text, startRow, -1);
        }

        public int ImportByKey(string text, int startRow, int endRow)
        {
            var setting = new DataFormatSetting()
            {
                StartRow = startRow,
                EndRow = endRow,
                IsSpecifyMappingMethod = true,
                MappingMethod = MappingMethod.Key
            };

            return _importer.Import(text, _dataFormat, setting);
        }
    }
}
